package com.synjones.handsetS8.ild.pages;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.synjones.handsetS8.R;
import com.synjones.handsetS8.ild.net.api.FileApi;
import com.synjones.handsetS8.ild.net.common.NetCallback;
import com.synjones.handsetS8.ild.net.common.NetResponse;
import com.synjones.handsetS8.ild.net.model.NetResult;
import com.synjones.handsetS8.ild.net.model.UploadImageResult;
import com.synjones.handsetS8.ild.utils.FileUtil;

import java.io.IOException;

;

/**
 * 拍照
 */
public class PhotoGraphActivity extends AppCompatActivity implements View.OnClickListener {
    public static final int ID_CARD_PHOTO = 0x00000001;//头像面
    public static final int ID_CARD_EMBLEM = 0x00000002;//国徽面
    public static final int ID_CARD_CUT_PHOTO = 0x00000003;//头像面剪切
    public static final int ID_CARD_CUT_EMBLEM = 0x00000004;//国徽面剪切

    private ImageView ivBack, ivIdCardPhoto, ivIdCardEmblem;
    private TextView tvTitle;
    private RelativeLayout rlIdCardPhotoGraph, rlIdCardEmblemGraph;
    private Button btnNext;

    //身份证信息
    private String workerName;
    private String idCardNo;
    private String idCardSidePhoto;
    private String idCardSideEmblem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_photo_graph);
        init();
    }


    private void init() {
        tvTitle = (TextView) findViewById(R.id.tv_title);

        tvTitle.setText("拍照");

        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);

        rlIdCardPhotoGraph = (RelativeLayout) findViewById(R.id.rl_id_card_photo_graph);
        rlIdCardEmblemGraph = (RelativeLayout) findViewById(R.id.rl_id_card_emblem_graph);

        ivIdCardPhoto = (ImageView) findViewById(R.id.iv_id_card_photo);
        ivIdCardEmblem = (ImageView) findViewById(R.id.iv_id_card_emblem);
        btnNext = (Button) findViewById(R.id.btn_next);

        rlIdCardPhotoGraph.setOnClickListener(this);
        rlIdCardEmblemGraph.setOnClickListener(this);
        btnNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.iv_back:
                finish();
                return;
            //拍摄头像面
            case R.id.rl_id_card_photo_graph:
                startIdCardPhotoGraph();
                break;
            //拍摄国徽面
            case R.id.rl_id_card_emblem_graph:
                startIdCardPhotoEmblem();
                break;
            //下一步
            case R.id.btn_next:
                toNext();
                break;
            default:
                break;
        }
    }

    /**
     * 下一步
     */
    private void toNext() {
        Intent intent = new Intent(PhotoGraphActivity.this, ManualInputActivity.class);
        intent.putExtra("worker_name", workerName);
        intent.putExtra("id_card_no", idCardNo);
        intent.putExtra("id_card_side_photo", idCardSidePhoto);
        intent.putExtra("id_card_side_emblem", idCardSideEmblem);
        startActivity(intent);
    }

    /**
     * 拍摄头像面
     */
    private void startIdCardPhotoGraph() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, ID_CARD_PHOTO);
    }

    /**
     * 拍摄国徽面
     */
    private void startIdCardPhotoEmblem() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, ID_CARD_EMBLEM);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case ID_CARD_PHOTO:
                    cutSidePhoto(data);
                    break;
                case ID_CARD_EMBLEM:
                    cutSideEmblem(data);
                    break;
                case ID_CARD_CUT_PHOTO:
                    parseSidePhoto(data);
                    break;
                case ID_CARD_CUT_EMBLEM:
                    parseSideEmblem(data);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 剪切头像面
     *
     * @param data
     */
    private void cutSidePhoto(Intent data) {
        Bundle extras = data.getExtras();
        //获取图片信息
        Bitmap bitmap = (Bitmap) extras.get("data");
        ivIdCardPhoto.setImageBitmap(bitmap);
        //保存图片
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp";
        try {
            String filePath = FileUtil.saveImage(bitmap, path);
            System.out.println(filePath);
            FileApi.uploadImage(filePath, new NetCallback<NetResult<UploadImageResult>>() {
                @Override
                protected void onComplete(NetResponse<NetResult<UploadImageResult>> netResponse) {
                    if (netResponse.isSuccess) {
                        idCardSidePhoto = netResponse.data.getData().getSrc();
                        Toast.makeText(PhotoGraphActivity.this, "头像面上传成功", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 剪切国徽面
     *
     * @param data
     */
    private void cutSideEmblem(Intent data) {
        Bundle extras = data.getExtras();
        //获取图片信息
        Bitmap bitmap = (Bitmap) extras.get("data");
        //Glide.with(this).load(bitmap).into(ivIdCardEmblem);
        ivIdCardEmblem.setImageBitmap(bitmap);
        //保存图片
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp";
        try {
            String filePath = FileUtil.saveImage(bitmap, path);
            FileApi.uploadImage(filePath, new NetCallback<NetResult<UploadImageResult>>() {
                @Override
                protected void onComplete(NetResponse<NetResult<UploadImageResult>> netResponse) {
                    if (netResponse.isSuccess) {
                        idCardSideEmblem = netResponse.data.getData().getSrc();
                        Toast.makeText(PhotoGraphActivity.this, "国徽面上传成功", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析头像面
     *
     * @param data
     */
    private void parseSidePhoto(Intent data) {

    }

    /**
     * 解析国徽面
     *
     * @param data
     */
    private void parseSideEmblem(Intent data) {

    }
}